<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">商品</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">添加相册</a></el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图  -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>新建相册</span>
        <div style="float:right">
            <el-button size="small" type="primary"  @click="$router.go(-1)" plain>返回</el-button>
        </div>
      </div>
      <div class="box-item">
        <!-- 添加分类表单  -->
        <el-form :model="addAlbumForm" :rules="addAlbumRules" ref="addAlbnmRef" :label-width="formLabelWidth" class="demo-ruleForm">
          <el-form-item label="相册名称：" prop="name" :label-width="formLabelWidth">
              <el-input v-model="addAlbumForm.title"></el-input>
          </el-form-item>

          <el-form-item label="相册封面" :label-width="formLabelWidth">
                <el-upload
                  class="avatar-uploader"
                  action="http://localhost:88/api/goods/youthOrange/uploadOss/upload?host=pholoAlbem"
                  list-type="picture-card"
                  :on-success="successAliyun"
                  :before-upload="beforeAvatarUpload"
                  >
                 <img v-if="addAlbumForm.image" :src="addAlbumForm.image" class="avatar">
                </el-upload>
          </el-form-item>

          <el-form-item label="相册描述" :label-width="formLabelWidth">
            <el-input v-model="addAlbumForm.desc" autocomplete="off" type="textarea" :autosize="{ minRows: 4, maxRows: 6  }"></el-input>
          </el-form-item>

          <el-form-item label="排序：">
            <el-input v-model="addAlbumForm.sort" type="number" min="1"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="onSubmit">提交</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>

export default {
  data () {
    return {
      formLabelWidth: '170px',
      // 添加分类表单
      addAlbumForm: {},
      // 添加分类规则
      addAlbumRules: {
        name: [{ required: true, messages: '请输入分类名称', trigger: 'blur' }]
      },
      // 模板集合
      templatesOptions: []
    }
  },
  methods: {
    // 上传之前的判断
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 上传成功
    successAliyun (response) {
      this.addAlbumForm.image = response.data.imgUrl
    },
    // 提交到后端，将数据保存到数据库
    async onSubmit () {
      const { data: res } = await this.$http.post(`/goods/album/add`, this.addAlbumForm)
        this.$message.success('添加成功')
        this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>